/**
 * 
 * 摇杆控制器
 * 
 */
class Rocker extends egret.DisplayObjectContainer {
    private box_png: egret.Bitmap = Utils.createBitmapByName("rocker_box_png");
    private item_png: egret.Bitmap = Utils.createBitmapByName("rocker_item_png");
    /** 保存摇杆触摸起始位置 */
    private startPoint: egret.Point = new egret.Point();
    /** 摇杆移动后位置 */
    private movePoint: egret.Point = new egret.Point();

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        /** 创建摇杆控制器 */
        this.drawRocker();
        /** 给摇杆控制器绑定事件 */
        this.rockerBindTouch();

    }
    /** 绘制摇杆控制器 */
    private drawRocker() {
        this.addChild(this.box_png);
        this.addChild(this.item_png);
        Utils.anchorCenter(this.box_png);
        Utils.anchorCenter(this.item_png);

        this.box_png.alpha = .3;
        this.item_png.scaleX = this.item_png.scaleY = this.box_png.scaleY = this.box_png.scaleX = .7;

        this.item_png.x = this.box_png.x = this.stage.stageWidth * .15;
        this.item_png.y = this.box_png.y = this.stage.stageHeight * .8;

        this.dispatchEvent(new egret.Event("rocker_item_start"));

    }
    /** 给摇杆控制器绑定拖动事件 */
    private rockerBindTouch() {
        this.item_png.touchEnabled = true;
        this.item_png.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchStart, this);
    }
    /**
     * 利用多点触控的原理, 触摸开始时把事件id赋值给显示对象
     */
    private touchStart(evt: egret.TouchEvent) {
        /** 拖动事件开始时, 给显示对象绑定name */
        this.item_png.name = evt.touchPointID.toString();
        
        this.startPoint.x = evt.stageX;
        this.startPoint.y = evt.stageY;
        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
    }

    private touchMove(evt: egret.TouchEvent) {
        /** 拖动事件时 判断显示对象的name */
        if(evt.touchPointID.toString() != this.item_png.name) return;

        
        this.movePoint.x = evt.stageX;
        this.movePoint.y = evt.stageY;

        const computed: number = egret.Point.distance(this.startPoint, this.movePoint);

        const angle: number = Math.atan2(evt.stageY - this.startPoint.y, evt.stageX - this.startPoint.x);

        if (computed < this.box_png.anchorOffsetX * .7) {
            this.item_png.x = this.box_png.x + evt.stageX - this.startPoint.x;
            this.item_png.y = this.box_png.y + evt.stageY - this.startPoint.y;
        } else {
            this.item_png.x = Math.cos(angle) * this.box_png.anchorOffsetX * .7 + this.box_png.x;
            this.item_png.y = Math.sin(angle) * this.box_png.anchorOffsetY * .7 + this.box_png.y;
        }

        this.dispatchEventWith("rocker_item_move", false, angle);

    }

    private touchEnd(evt:egret.TouchEvent) {
        /** 结束拖动事件时 判断显示对象的name */
        if(evt.touchPointID.toString() != this.item_png.name) return;

        this.item_png.x = this.box_png.x;
        this.item_png.y = this.box_png.y;
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);

        this.dispatchEvent(new egret.Event("rocker_item_stop"));
    }


}